/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dominio;

import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

/**
 *
 * @author Sebastian
 */
public class Bala implements Runnable{

    private int x;
    private int y;
    private int ancho;
    private int alto;
    private int direccion;
    private String [] rutas;
    private ImageIcon imagen;
    private LinkedList<Obstaculo> lstObs;
    private boolean estado = true;
    
    public Bala(){}

    public Bala(int x, int y, int ancho, int alto, String [] rutas, LinkedList<Obstaculo> lstObs) {
        this.x = x;
        this.y = y;
        this.ancho = ancho;
        this.alto = alto;
        this.rutas = rutas;
        this.imagen = new ImageIcon(getClass().getResource(rutas[0]));
        this.lstObs = lstObs;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getAncho() {
        return ancho;
    }

    public void setAncho(int ancho) {
        this.ancho = ancho;
    }

    public int getAlto() {
        return alto;
    }

    public void setAlto(int alto) {
        this.alto = alto;
    }

    public int getDireccion() {
        return direccion;
    }

    public void setDireccion(int direccion) {
        this.direccion = direccion;
    }

    public String[] getRutas() {
        return rutas;
    }

    public void setRutas(String[] rutas) {
        this.rutas = rutas;
    }

    public ImageIcon getImagen() {
        return imagen;
    }

    public void setImagen(ImageIcon imagen) {
        this.imagen = imagen;
    }

    public LinkedList<Obstaculo> getLstObs() {
        return lstObs;
    }

    public void setLstObs(LinkedList<Obstaculo> lstObs) {
        this.lstObs = lstObs;
    }

    public boolean getEstado() {
        return estado;
    }

    public void setEstado(boolean estado) {
        this.estado = estado;
    }
    
    public void verificarCruces() {
        int i = 0;
        boolean colision = false;
        while (i < this.lstObs.size() && !colision) {
            if (this.x + this.ancho >= this.lstObs.get(i).getX()
                    && (this.x <= this.lstObs.get(i).getX() + this.lstObs.get(i).getAncho())) {
                if (this.y + this.alto >= this.lstObs.get(i).getY()
                        && this.y <= (this.lstObs.get(i).getY() + this.lstObs.get(i).getAlto())) {
                    colision = true;
                }
            } 
            else if (this.x >= this.lstObs.get(i).getX()
                    && this.x <= (this.lstObs.get(i).getX() + this.lstObs.get(i).getAncho())) {
                if (this.y + this.alto >= this.lstObs.get(i).getY()
                        && this.y <= (this.lstObs.get(i).getY() + this.lstObs.get(i).getAlto())) {
                    colision = true;
                }
            } 
            else if (this.x + this.ancho >= this.lstObs.get(i).getX()
                    && this.x <= (this.lstObs.get(i).getX() + this.lstObs.get(i).getAncho())) {
                if (this.y >= this.lstObs.get(i).getY()
                        && this.y <= this.lstObs.get(i).getY() + this.lstObs.get(i).getAlto()) {
                    colision = true;
                }
            } 
            else {
                if (this.x + this.ancho >= this.lstObs.get(i).getX()
                        && this.x <= this.lstObs.get(i).getX() + this.lstObs.get(i).getAncho()) {
                    if (this.y + this.alto >= this.lstObs.get(i).getY()
                            && this.y <= this.lstObs.get(i).getY() + this.lstObs.get(i).getAlto()) {
                        colision = true;
                    }
                }
            }
            i++;
        }
        if (colision) {
            i--;
            if (i > 3) {
                this.lstObs.remove(i);
                this.imagen = new ImageIcon(getClass().getResource(this.rutas[1]));
                this.alto=32;
                this.ancho=32;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Bala.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            this.estado = false;
            Thread.currentThread().stop();
        }
    }
    
    @Override
    public void run() {
        while (true) {
            try {
                if (this.direccion == 0) {
                    this.x += 10;
                } 
                else if (this.direccion == 1) {
                    this.x -= 10;
                } 
                else if (this.direccion == 2) {
                    this.y -= 10;
                } 
                else {
                    this.y += 10;
                }
                this.verificarCruces();
                Thread.sleep(100);
            } 
            catch (InterruptedException ex) {
                Logger.getLogger(Bala.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}